<?php
include_once 'include.php';

$order1 = $_POST['order1'];
$order2 = $_POST['order2'];
$uid = $_POST['uid'];
$user = $_POST['user'];

$ImageObj = new Image();

$sql = "UPDATE `".$ImageObj->get_table_name()."` SET `order`= CASE WHEN `order`= '$order1' THEN '$order2' WHEN `order`= '$order2' THEN '$order1' END WHERE `order` IN ($order1, $order2)";
if(mysql_query($sql)){
	$html = '';
	$html .= '<tr>
				<td colspan="5"><b>Order</b></td>
			  </tr>';
	
	$res = mysql_query("SELECT * FROM `".$ImageObj->get_table_name()."` WHERE `uid` = '$uid' ORDER BY `order` ASC");
	while($row = mysql_fetch_assoc($res)){
		$html .= '<tr id="row_'.$row['id'].'">';
		$html .=	'<td width="'.( $user == "owner" ? '7%':'5%' ).'" class="'.( $user == "owner" ? 'CenterAlign':'' ).'">&nbsp;</td>';
		$html .=	'<td width="'.( $user == "owner" ? '23%':'25%' ).'" align="center" class="'.( $user == "owner" ? 'CenterAlign':'' ).'">';
		$html .=	    '<input type="hidden" name="image_order" value="'.$row['order'].'">';
		$html .=	    '<img src="'.( $user == "owner" ? 'administrator/':'' ).$row['thumbnailurl'].'" alt="">';
		$html .=	'</td>';
		$html .=	'<td width="" valign="middle">Caption ';
		$html .=	    '<input type="text" size="'.( $user == "owner" ? '40':'45' ).'" name="caption" value="'.$row['caption'].'" />&nbsp;';
		$html .=	    '<a href="javascript:void(0)" onclick="save_caption($(this), '.$row['id'].')"><img alt="save" src="'.( $user == "owner" ? 'administrator/':'' ).COMPONENT_PATH.'images/save.jpg"></a>';
		$html .=	    '<span class="Loader"><img alt="save" src="'.( $user == "owner" ? 'administrator/':'' ).COMPONENT_PATH.'images/loading-icon.gif"></span>';
		$html .=	'</td>';
		$html .=	'<td width="5%" valign="middle"><a href="'.( $user == "owner" ? 'administrator/':'' ).$row['photourl'].'" rel="facebox"><img alt="zoom" src="'.( $user == "owner" ? 'administrator/':'' ).COMPONENT_PATH.'images/zoom.png"></a></td>';
		$html .=	'<td width="5%" valign="middle" class="'.( $user == "owner" ? 'CenterAlign':'' ).'">';
		$html .=	    '<a href="javascript:void(0)" class="Delete" onclick="delete_image('.$row['id'].')">';
		$html .=	    	'<img alt="delete" src="'.( $user == "owner" ? 'administrator/':'' ).COMPONENT_PATH.'images/cross.png">';
		$html .=	    '</a>';
		$html .=	'</td>';
		$html .= '</tr>';
	}
	echo json_encode(array("error"=>"0","html"=>$html));
}
else echo json_encode(array("error"=>"1","html"=>"error"));
?>